namespace SimpleWebServices.Models
{
    using System;
    using System.Xml.Serialization;
    using SimpleWebServices.Data;
    using SimpleWebServices.Web;
    using SimpleWebServices.Web.UI.Html;
    using SimpleWebServices.Web.UI.Html.Elements;

    [XmlRoot("template")]
    public sealed class HtmlTemplate : Resource
    {
        [XmlElement("div")]
        public Division Div
        {
            get;
            set;
        }

        [XmlIgnore]
        public override string Entity
        {
            get
            {
                Entity value = new Entity(base.Entity);
                value.Append(this.Div);

                return value;
            }
        }

        public void SetText(string id, string value)
        {
            if (null == id)
            {
                throw new ArgumentNullException("id");
            }
            else if (0 == id.Length)
            {
                throw new ArgumentOutOfRangeException("id");
            }

            HtmlTemplate.SetText(this.Div[id] as IInnerElements, value);
        }

        private static void SetText(IInnerElements obj, string value)
        {
            if (null == obj)
            {
                throw new ArgumentNullException("obj");
            }

            if (0 != obj.Elements.Count)
            {
                obj.Elements.Clear();
            }

            if (null != value)
            {
                obj.Elements.Add(new TextString(value));
            }
        }
    }
}